{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用梯度上升法求解主成分\n",
    "> 主成分即w,是一个m行1列的向量\n",
    "## 构造数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.empty((100, 2))\n",
    "X[:, 0] = np.random.uniform(0., 100., size=100) # 第0维：特征值\n",
    "X[:, 1] = 0.75 * X[:, 0] + 3. + np.random.normal(0, 10., size=100) # 第1维：目标值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 2)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAD4CAYAAAAJmJb0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAbDUlEQVR4nO3df6zddX3H8efb21u8xblbpSrc0rVGgiJM624cs4txxQwUlcb5A0M2MlmaJW4qmkqZydBkxhqMP5Y5lgZ0uBApAitMncxRzDYSO28tCogMBgi9dFI3LhqseAvv/XG+l56e+/2e8z3n++vz/ZzXIzHt+dH7/Zz7xff3c97f9+f9MXdHRETi9JymByAiItVRkBcRiZiCvIhIxBTkRUQipiAvIhKxFU0PoNsJJ5zg69evb3oYIiKtsm/fvp+6+5q014IK8uvXr2dubq7pYYiItIqZ/TjrNaVrREQipiAvIhIxBXkRkYgpyI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Jf+dL33mqM91Iuu83gz8UVJlcybwxFJaZ2ju3vr/AW+mszPVf9Npgdz4mCr4jL9L5+vaD4A7kv+9mU6e+lbgvuTPFzQ91go++xuAryV/fymdZnf3A18Fjmt6fBV83lcDc8m53g2sjv08Ax8HfgTcBfwDnZ5YUZ1r4Ct07jks0pmpX5R1Xumka76QxLQ76VQejXRctTUQEYlYDOkaERHJoCAvIhIxBXkRkYgpyIuIRExBXkQkYgryIiIRU5AXEYnY/wO7z0Ws6WMVogAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.axis('on') # 打开坐标轴显示，关闭时off\n",
    "plt.scatter(X[:, 0], X[:, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 减去平均值demean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def demean(X):\n",
    "    return X - np.mean(X, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_demean = demean(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAD4CAYAAAAJmJb0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAaBklEQVR4nO3df6wdZZ3H8c+X9oK3uru3ShG4bbdN7KKwbCx7w7LpP4o/ioDQuBpxiRIlaUzYBF2stJCsbCLxGhJhja4J0U0wMQFWtHRFg0DxjyUBvaUgqdi1kVV6i2tNqBqp2Jbv/nHm0NPTmTlz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xmyTNS7rXzK5Vr4rsAzW1r0o3SrrbzD4rabd6N7/WY1kDAGgx0jUA0GIEeQBoMYI8ALQYQR4AWowgDwAtRpAHgBYjyANAi/0/0ncys5wa2KQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_demean[:, 0], X_demean[:, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.2737367544323206e-15"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X_demean[:, 0]) # 第0维度均值基本是0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.851497230149107e-15"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X_demean[:, 1]) # 第1维度均值基本是0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 梯度上升法的实现\n",
    "### 1.`f(X)`的表达式，见7.1\n",
    "![主成分分析的最终目标](../01-什么是PCA/images/主成分分析的最终目标.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(w, X):\n",
    "    return np.sum((X.dot(w)**2)) / len(X) # X必须是已经去平均化的矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.`df`的表达式\n",
    "![PCA的最终求导表达式](../02-使用梯度上升法求解PCA问题/images/PCA的最终求导表达式.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def df_math(w, X):\n",
    "    return X.T.dot(X.dot(w) * 2. / len(X))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def df_debug(w, X, epsilon=0.0001):\n",
    "    res = np.empty(len(w))\n",
    "    for i in range(len(w)):\n",
    "        w_1 = w.copy()\n",
    "        w_1[i] += epsilon\n",
    "        w_2 = w.copy()\n",
    "        w_2[i] -= epsilon\n",
    "        res[i] = (f(w_1, X) - f(w_2, X)) / (2 * epsilon)\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''把向量转换为单位方向向量'''\n",
    "def direction(w):\n",
    "    return w / np.linalg.norm(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gradient_ascent(df, X, initial_w, eta, n_iters = 1e4, epsilon=1e-8): # 注意是ascent(梯度上升)不是descent(梯度下降)了\n",
    "    \n",
    "    w = direction(initial_w)\n",
    "    cur_iter = 0\n",
    "\n",
    "    while cur_iter < n_iters:\n",
    "        gradient = df(w, X)\n",
    "        last_w = w\n",
    "        # 这里与梯度下降法不同，关键之处\n",
    "        w = w + eta * gradient\n",
    "        # 转换为单位方向向量\n",
    "        w = direction(w)\n",
    "        if(abs(f(w, X) - f(last_w, X)) < epsilon):\n",
    "            break\n",
    "            \n",
    "        cur_iter += 1\n",
    "\n",
    "    return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.53527228,  0.24809709])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "initial_w = np.random.random(X.shape[1])\n",
    "initial_w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "eta = 0.001"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> PCA问题不能使用StandardScaler标准化数据,因为标准化会影响数据的分布"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.78316652,  0.62181203])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gradient_ascent(df_debug, X_demean, initial_w, eta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.78316652,  0.62181203])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gradient_ascent(df_math, X_demean, initial_w, eta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAHc1JREFUeJzt3X+wXWV97/H3l3ACh6JEIAieEJNWjBcERc6gndSpUCUINKQgFUsVwTZjBxxADCQw8qODTWy8/LCt3EZwxGvmBgo0xAudgAQ7lBHahEBpmsSmgMBJroQxodVEOYTv/WPtDTvnrH322nuvH89a6/OayXD2z/XsxTnf9ezv832ex9wdERGpvn2KboCIiORDAV9EpCYU8EVEakIBX0SkJhTwRURqQgFfRKQmFPBFRGpCAV9EpCYU8EVEamLfohvQ6tBDD/UZM2YU3QwRkVJZt27dK+4+tdPzggr4M2bMYO3atUU3Q0SkVMzsp0mep5SOiEhNKOCLiNSEAr6ISE0o4IuI1IQCvohITQRVpSMiUhUr14+wdPVmtu7czbumDLJgzizmHT9UaJsU8EVEUrZy/QiL7n2G3aN7ABjZuZtF9z4DUGjQV0pHRCRlS1dvfjPYN+0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HNX4Nx+4tYCm5eUh4P3ufhzwE2ARgJkdDZwLHAOcCnzLzCa1fZcSa3yuvyH6/3408JnG56+y14HL3f1/AB8BLmp85oXAw+5+FPBw43bVXQJsbLn9deCmxjnYAXyhkFZ1SQE/mZuAK4DWAY8zge955HFgipkdUUjrMubuD7r7642bjwPTGj+fCaxw91+7+3PAFuDEItqYgxOBLe7+rLu/Bqwg+vyV5e7b3P3Jxs//TRTwhog+9x2Np90BzCumhfkws2nA6cBtjdsGnAzc3XhKac6BAn4HZjYXGHH3p8c8NAS82HL7pcZ9VXch8A+Nn+t0Dur0WccxsxnA8cATwDvdfRtEFwXgsOJaloubiTp8bzRuHwLsbOkEleZ3QRugAGb2Q+DwmIeuBq4CTol7Wcx9pS15mugcuPt9jedcTfQ1f3nzZTHPL+056KBOn3UvZnYgcA9wqbv/V9TBrQczOwN42d3XmdnHmnfHPLUUvwsK+IC7fzzufjM7FpgJPN34JZ8GPGlmJxJd1Y9sefo0YGvGTc1Mu3PQZGbnA2cAv+dv1fJW6hx0UKfP+iYzGyAK9svd/d7G3T8zsyPcfVsjjflycS3M3GxgrpmdBuwPvJ2oxz/FzPZt9PJL87uglM4E3P0Zdz/M3We4+wyiP/oPufv/A1YBn2tU63wEeLX5NbdqzOxU4EpgrrvvanloFXCume1nZjOJBrD/uYg25uBfgKMa1RmTiQarVxXcpkw1ctW3Axvd/caWh1YB5zd+Ph+4L++25cXdF7n7tMbf/7nAGnc/D3gE+FTjaaU5B+rh9+4B4DSigcpdwAXFNidTfw3sBzzU+KbzuLt/0d03mNldwL8TpXoucvc9BbYzM+7+upldDKwGJgHfcfcNBTcra7OBzwLPmNlTjfuuApYAd5nZF4gq184pqH1FuhJYYWY3AOuJLozB00xbEZGaUEpHRKQmFPBFRGpCAV9EpCYU8EVEakIBX0SkJhTwRURqQgFfRKQmFPBFRGri/wMXtI93UOx1GgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "w = gradient_ascent(df_math, X_demean, initial_w, eta)\n",
    "plt.scatter(X_demean[:, 0], X_demean[:, 1])\n",
    "plt.plot([0, w[0]*30], [0, w[1]*30], color='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 下面使用一个纯线性的数据进行PCA测试，可以看到计算结果是很精准的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2 = np.empty((100, 2))\n",
    "X2[:, 0] = np.random.uniform(0., 100.0, size=100)\n",
    "X2[:, 1] = 0.75 * X2[:, 0] + 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAGsRJREFUeJzt3X+Q3PV93/HnS8cRVsTuCSOodEgVThkcYxVwbkCNOhkibGNjAyoBYxlapWVG/aNNbcejICaaIs/QQa7SYHfGQ0Y1TpRCxC/Li2J7rDICJhOmUnzygQ8ZVDDGQisFnW3OTs1NOI53/9jvnk+r3dvv3u13b/e7r8fMzd73e9/VfdZf8fJH7+/nhyICMzPrfosWugFmZtYaDnQzs5xwoJuZ5YQD3cwsJxzoZmY54UA3M8sJB7qZWU440M3MciJVoEv6nKTDkp6XtFvSWZIulHRQ0kuSHpZ0ZtaNNTOz+tRopqikQeBvgfdHxISkR4BvA9cCeyLiIUl/BjwXEffN9mede+65sWrVqta03MysRxw6dOgnEbG00XVnpPzzzgAKkiaBxcAJYB3w6eTnu4BtwKyBvmrVKoaHh1P+SjMzA5D04zTXNSy5REQJ+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Z4qameVEJ5dczMysCQ50M7OccKCbmeWEA93MLCcc6GZmOeFANzPLCQe6mVlOONDNzHLi/wPDl9uXBsiOagAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2[:, 0], X2[:, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2_demean = demean(X2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "w2 = gradient_ascent(df_math, X2_demean, initial_w, eta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAHsBJREFUeJzt3XuYlHX9//Hnm2HRxdOieGJhf/A11CRUbAOKX33NE+YJJE+kSckvfva1slIElBQLE9vLSMr6/kj9RkUimo1gGh6ptBaDVsQFSdQCBhI1VlI2XZb374+5Z5tdZndnmfPcr8d1cbFzzz1zf+6Lixcf3vfnYO6OiIiUv16FboCIiOSHAl9EJCQU+CIiIaHAFxEJCQW+iEhIKPBFREJCgS8iEhIKfBGRkFDgi4iERO9CNyBZ//79ffDgwYVuhohISVm1atWb7n5od+cVVeAPHjyYlStXFroZIiIlxcz+ls55KumIiIRE1gLfzCJm1mBmDwevh5jZCjN72czuM7M+2bqWiIj0XDZ7+FcD65Je3wbMdfehwHZgchavJSIiPZSVwDezgcDZwF3BawNOAR4ITlkAjM/GtUREZO9kq4f/PeA6YHfw+hCgyd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H1s8Voo058idgaDBiow/xB9RLCtymnAjq1ncD69z9u0lvLQEmBT9PAh7Kd9tyyd1nuPvA4O/zJcBT7n4p8DRwQXBa2d13KqHt4e+lR4CziD+43Al8vrDNyaofAPsAjwf/s6l39yvdvdHMFgNriZd6rnL31gK2M6vcfZeZfQlYBkSAe9y9scDNypUxwGeBNWb2fHDsemAOsNjMJhMflXZhgdqXb9OARWY2G2gg/o9hWdNMWxGRkFBJR0QkJBT4IiIhocAXEQkJBb6ISEgo8EVEQkKBLyISEgp8EZGQUOCLiITE/wdrhCnKwcO0MgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2_demean[:, 0], X2_demean[:, 1])\n",
    "plt.plot([0, w2[0]*30], [0, w2[1]*30], color='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.80000044,  0.59999941])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
